"""
@filename=persistence.py
@version=0.1.0.20240110
@author=amnotgcs
@createTime=20240110 20:36
@lastModifiedTime=20240110 20:36
@description=数据持久化
"""


from config import PERSISTENCE_HTML_FOLDER, PERSISTENCE_PDF_FOLDER
from utils import safe_filename


def html_to_file(html: str, filename: str) -> None:
    """将HTML文件保存到本地"""
    filename = safe_filename(filename)
    path_for_save_html = f'{PERSISTENCE_HTML_FOLDER}/{filename}.html'
    with open(path_for_save_html, 'wt', encoding='UTF-8') as file:
        file.write(html)


def pdf_to_file(pdf: bytes, filename: str) -> None:
    """将PDF文件保存到本地"""
    filename = safe_filename(filename)
    path_for_save_pdf = f'{PERSISTENCE_PDF_FOLDER}/{filename}.pdf'
    with open(path_for_save_pdf, 'wb') as file:
        file.write(pdf)
